#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0

typedef struct _Tag_Person
{
    char name[64];
    int age;
}Person;

typedef struct Person MyPerson

#endif

struct Person
{
    char name[64];
    int age;
} person = {"John", 100}; 
//定义类型时定义变量

struct 
{
    char name[64];
    int age;
} obama = {"Obama", 99};


void test01()
{
    // strcpy(person.name, "Lily");
    // person.age = 100;

    printf("Name:%s Age:%d\n", person.name, person.age);
}

//单个结构体变量
void test02()
{
    //在栈上分配结构体变量空间
    struct Person person = {"Hameimei", 25};
    printf("Name:%s Age:%d\n", person.name, person.age);

    //在堆上分配结构体变量空间
    struct Person *pperson = malloc(sizeof(struct Person));
    strcpy(pperson->name, "Lilei");
    pperson->age = 30;
    printf("Name:%s Age:%d\n", pperson->name, pperson->age);
}

void printPersons(struct Person *persons, int len)
{
    for (int i = 0; i < len; ++i)
    {
        printf("Name:%s Age:%d\n", persons[i].name, persons[i].age);
    }
}

//多个结构体变量
void test03()
{
    //在栈上分配结构体数组空间
    struct Person persons[] = {
        {"aaa",  20},
        {"bbb", 30},
        {"ccc",  40},
        {"ddd", 50},
        {"eee", 60},
        {"fff", 70}
    };
    int len = sizeof(persons) / sizeof(struct Person);
    printPersons(persons, len);  

    //在堆上分配结构体数组空间
    struct Person *ps = malloc(sizeof(struct Person)*6);
    for (int i = 0; i < 6; ++i)
    {
        sprintf(ps[i].name, "name_%d", i+1);
        ps[i].age = 100 + i;
    }
    printPersons(ps, 6);
}

int main(int argc, char const *argv[])
{
    test01();
    test02();
    test03();
    return 0;
}
